<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>元素值大于变化阈值的子数组</title>
</head>
<body>
  <script>
    function fn(nums, threshold) {
      const n = nums.length;
      const left = new Array(n).fill(-1);
      const right = new Array(n).fill(n);
      const stack = [];

      for (let i = 0; i < n; i++) {
        while (stack.length && nums[last(stack)] >= nums[i]) stack.pop();
        left[i] = stack.length ? last(stack) : -1;
        stack.push(i);
      }

      stack.length = 0;
      for (let i = n - 1; i >= 0; i--) {
        while (stack.length && nums[last(stack)] >= nums[i]) stack.pop();
        right[i] = stack.length ? last(stack) : n;
        stack.push(i);
      }

      for (let i = 0; i < n; i++) {
        const k = right[i] - left[i] - 1;
        if (nums[i] > (threshold / k)) {
          return k;
        }
      }

      return -1
    }

    function last(stack) {
      return stack[stack.length - 1];
    }
    console.log(fn([1, 3, 4, 3, 1], 6))
  </script>
</body>
</html>